﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Activities;
using Blackbaud.AppFx.XmlTypes;
using Blackbaud.AppFx.XmlTypes.DataForms;
using System.Collections;

namespace BBECServiceBusAdapter.Activities
{
    public sealed class CallbackActivity : NativeActivity
    {
        #region Activity Arguments
        public InArgument<string> ResultCode { get; set; }
        public InArgument<string> ResultDescription { get; set; }
        public InArgument<string> ResultDetails { get; set; }
        public OutArgument<Exception> ActivityResult { get; set; }
        #endregion

        protected override void Execute(NativeActivityContext context)
        {
            try
            {
                // establish service

                //WV.JoshuaServices svc = new WV.JoshuaServices();
                //WV2.JoshuaWSDLGateway svc = new WV2.JoshuaWSDLGateway(); //
                ProcessCallback_localhost.ProcessCallback svc = new ProcessCallback_localhost.ProcessCallback();

                // establish result message
                //WV2.ResponseInfo result = new WV2.ResponseInfo();
                ProcessCallback_localhost.ResponseInfo result = new ProcessCallback_localhost.ResponseInfo();


                
                //svc.UseDefaultCredentials = true;
                svc.Credentials = new System.Net.NetworkCredential("", "");


                
                result.code = ResultCode.Get<string>(context);
                result.description = ResultDescription.Get<string>(context);
                
                // if there is an ErrorDetails property then get the items in it and populate the error details
                if (context.Properties.Find("ErrorDetails") != null)
                {
                    //List<WV2.ErrorDetailInfo> errorDetails = new List<WV2.ErrorDetailInfo>();
                    List<ProcessCallback_localhost.ErrorDetailInfo> errorDetails = new List<ProcessCallback_localhost.ErrorDetailInfo>();
                    foreach (Helper.ErrorDetailInfo detail in (List<Helper.ErrorDetailInfo>)context.Properties.Find("ErrorDetails"))
                    {
                        errorDetails.Add(new ProcessCallback_localhost.ErrorDetailInfo() { faultCode = detail.errorCode.ToString(), faultDescription = detail.errorDescription, faultLevel = detail.errorLevel.ToString() });
                        //errorDetails.Add(new WV2.ErrorDetailInfo() { faultCode = detail.errorCode.ToString(), faultDescription = detail.errorDescription, faultLevel = detail.errorLevel.ToString() });
                    }
                    
                    //result.Details = errorDetails.ToArray<WV2.ErrorDetailInfo>();
                    result.Details = errorDetails.ToArray<ProcessCallback_localhost.ErrorDetailInfo>();
                }

                // perform the callback  (TODO:  Check with WV Gateway folks if we can do this asynchronously)
                svc.PostConfirmation(context.WorkflowInstanceId.ToString(), result);

                // set a null exception, meaning all is good
                ActivityResult.Set(context, null);
            }
            catch (Exception e)
            {
                // return the exception so it can be used for logging
                ActivityResult.Set(context, e);
            }
        }
    }
}